{% load basiccomparisonfilters %}
{% load uploadify_tags %}

{% block script %}

  <script type="text/javascript">
	$(document).ready(function(){

		$(".dataset_metadata_toggle").live('click', function(e){
			e.preventDefault();

			if($(this).text() == "[Show]")
			{

				$(this).text("[Hide]");
				var href= $(this).attr("href");

			    	$(this).next().html(loadingHTML + "</br>");
			        $(this).next().load(href, function() {
			        $(this).show();
			        });

			}
			else
			{
				$(this).text("[Show]");
				var href= $(this).attr("href");
				$(this).next().hide();
			}
			return false;
		});

		$(".datafile_info_toggle").text("[Show]");
		$(".datafile_info_toggle").nextAll(".datafile_info").hide();
		var loadingHTML = "<img src='/site_media/images/ajax-loader.gif'/>";

		// metadata text / ajax toggle

		$(document).ready(function(){

			$(".datafile_info_toggle").live('click', function(e){
				e.preventDefault();

				if($(this).text() == "[Show]")
				{

					$(this).text("[Hide]");
					var href= $(this).attr("href");

			    	$(this).nextAll(".datafile_info").html(loadingHTML + "</br>");
			        $(this).nextAll(".datafile_info").load(href);
					$(this).nextAll(".datafile_info").show();
				}
				else
				{
					$(this).text("[Show]");
					var href= $(this).attr("href");
					$(this).nextAll(".datafile_info").hide();
				}
				return false;
			});
		});
	});

    $("#uploadify").live("allUploadsComplete", function(e, data){
	dataset_id = $(this).parents('.upload_files_container'
				    ).siblings('.dataset_id').val();

	$(this).parents('.datafile_list'
		       ).load("/ajax/datafile_list/" +
			      dataset_id + "/",function(){
				  $(document).ready(function(){

				      // file selectors
				      $(this).find('.dataset_selector_all').click(function() {
					  $(this).parent().find('.datafile_checkbox').attr("checked", "checked");

				      });

				  });

			      });


    });

    function disableEnterKey(e)
    {
	var key;
	if(window.event)
	    key = window.event.keyCode; //IE
	else
	    key = e.which; //firefox

	return (key != 13);
    }

    $('.upload_files_link').live('click', function( event ){

	$('.upload_files_container').each(function(index) {
	    $(this).html("");
	});

	$('.upload_files_link').each(function(index) {
	    $(this).show();
	    $(this).html('Upload Files To Dataset');
	});

	$(this).hide();

	dataset_id = $(this).siblings('.dataset_id').val();

	$(this).siblings(".upload_files_container").load("/ajax/upload_files/" + dataset_id + "/");


    });


    $('.filename_search').live('keyup', function(e) {
	e.preventDefault();
	if(e.keyCode == 13) {
	    dataset_id = $(this).siblings('.dataset_id').val();

	    $(this).parents('.datafile_list').load("/ajax/datafile_list/" + dataset_id + "/?filename=" + $(this).val(), function(){});
	}

    });


    // show hide various metadata divs

    $('#datasets').find('.dataset_info').end().find('h2').click(function() {
	$(this).next().toggle();
    });

    $('.dataset_info').find('.datafile_list').hide().end().find('.datafile_list_toggle').click(function() {
	$(this).next().toggle();
    });


    $('.pagelink').live('click', function( event ){
	var href = $(this).attr("href");
	html = $(this).html();
	$(this).html(html + " " + loadingHTML);
	$(this).parents('.datafile_list').load(href, function(){

		$(".dataset_selector_all").unbind('click');
		$(".dataset_selector_none").unbind('click');
		// file selectors
		$(document).find('.dataset_selector_all').click(function() {
		    $(this).parent().find('.datafile_checkbox').attr("checked", "checked");

		});

		$(document).find('.dataset_selector_none').click(function() {
		    $(this).parent().find('.datafile_checkbox').removeAttr("checked");

		});


	    $(this).html(html);

	})
	return false;
    });

    // default text set

    $(".dataset_metadata_toggle").text("[Show]");
    $(".dataset_metadata").hide();
    $(".datafile_list_toggle").text("[Show]");

    var loadingHTML = "<img src='/site_media/images/ajax-loader.gif'/>";

    // metadata text / ajax toggle

    // datafile list
    $(".datafile_list_toggle").toggle(function(){
	$(this).text("[Hide]");
	var href = $(this).attr("href");

	$(this).next().html(loadingHTML + "</br>");

	var datafile_info_container = $(this).next();

	// add initialisation for new datafile handlers as callback once ajax has loaded
	$(this).next().load(href,function() {
	    $(document).ready(function(){

		var datafile_info_selectors =
		    datafile_info_container.find('.datafile_info').hide().end().find('.datafile_info_toggle');
		datafile_info_selectors.text("[Show]");

		// datafile metadata
		datafile_info_selectors.toggle(function(){
		    $(this).text("[Hide]");
		    var href = $(this).attr("href");
		    //if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
		    //{
		    $(this).nextAll(".datafile_info").html(loadingHTML);
		    //}
		    $(this).nextAll(".datafile_info").load(href);

		},function(){
		    $(this).text("[Show]");
		});

		datafile_info_selectors.click(function() {
		    $(this).nextAll(".datafile_info").toggle();
		});
		// file selectors
		datafile_info_container.find('.dataset_selector_all').click(function() {
		    $(this).parent().find('.datafile_checkbox').attr("checked", "checked");

		});

		datafile_info_container.find(
		    '.dataset_selector_none'
		).click(function() {
		    $(this).parent().find('.datafile_checkbox'
					 ).removeAttr("checked");

		});

	    });
	});

    },function(){
	$(this).text("[Show]");
    });

    $('.dataset_checkbox').live('click', function( event ) {
	if ($(this).is(':checked')) {
	    $(this).parents('.dataset').find('.datafile_checkbox'
					    ).attr('disabled', true);
	    $(this).parents('.dataset').find('.filename_search').attr('disabled', true);
	} else {
	    $(this).parents('.dataset').find('.datafile_checkbox').removeAttr('disabled');
	    $(this).parents('.dataset').find('.filename_search').removeAttr('disabled');
	}
    });
    $("input[name$='show_search']").click(function() {
      if($(this).val() == "matches")
      {
        $(".dataset").hide();
        $(".search_match").show();
        $(".datafile_match").show();
      }  
      else
      {
        $(".dataset").show();
      }
    }); 
  </script>

{% endblock %}

<div id="experiment_datasets">
  <div class="download_entire_experiment">

  {% for p in protocols %}
    <p>
	{% if p.0 %}
	  <strong><a href="{{p.1}}">Download Entire Experiment ({{p.0|upper}})</a></strong><br/>
	{% else %}
	  <strong><a href="{{p.1}}">Download Entire Experiment</a></strong><br/>
	{% endif %}
    </p>

  {% endfor %}

  </div> <!-- download_entire_experiment -->

  {% if highlighted_datasets or file_matched_datasets %}
  <form name="search_select_form" id="search_form" method="post" action="">
    <div><label><input type="radio" name="show_search" value="all" checked>Show all</label></div>  
    <div><label><input type="radio" name="show_search" value="matches">Search matches only</label></div>  
  </form>
  {% endif %}
  <form method="POST" action="{% url tardis.tardis_portal.download.download_datafiles %}" target="_blank">
    <input type="hidden" name="expid" value="{{experiment.id}}"/>

    <div id="datasets">
      {% for dataset in datasets %} <!-- experiment.dataset_set.all %}-->
	<div class="dataset{% if dataset.pk in highlighted_datasets %} search_match{% endif %}{% if dataset.pk in file_matched_datasets %} datafile_match{% endif %}">
        <div class="dataset_title">
        {% if dataset.pk in highlighted_datasets %}
          <span style="background-color: #FFFF00">
        {% endif %}
	    <input type="checkbox" class="dataset_checkbox" name="dataset" value="{{dataset.id}}" />
        <strong><span style="color: #5e5e5e">DATASET {{ forloop.counter }}</span></strong>
        {% if dataset.pk in highlighted_datasets %}
          </span>
        {% endif %}
	  </div> <!-- dataset title -->

	  <div class="dataset_info">
	    <div class="dataset_description">
	      <strong>Dataset Description</strong>: {{ dataset.description|safe }} <br />
	    </div> <!-- dataset_description -->

	    <div class="dataset_extrainfo">

		<strong>Dataset Metadata: </strong>

		{% if has_write_permissions %}{% if not experiment.public %}{% if not dataset.immutable %}
		<a href="{% url tardis.tardis_portal.views.add_dataset_par dataset.id %}"
		 class="jqModalDsA{{dataset.id}}">[Add]</a>
		<script type="text/javascript">
		$('#jqmAlertDsA{{dataset.id}}').die();
		$('#jqmAlertDsA{{dataset.id}}').jqm({
		  trigger: '.jqModalDsA{{dataset.id}}',
		  ajax: '@href',
		  target: '#jqmAlertContentdatasetA{{dataset.id}}',
		  overlay: 0
		  });
		</script>
		<div class="jqmAlert" id="jqmAlertDsA{{dataset.id}}">

			<div class="jqmAlertWindow">
			    <div class="jqmAlertTitle clearfix">
			    <h1>Add Parameters</h1><a href="#" class="jqmClose"><em>Close</em></a>
			  </div>

			  <div class="jqmAlertContent" id="jqmAlertContentdatasetA{{dataset.id}}">
			  <p>Please wait... <img src="/site_media/images/busy.gif" alt="loading" /></p>
			  </div>
			</div>
		</div>
		{% endif %}{% endif %}{% endif %}

		<a class="dataset_metadata_toggle" href="/ajax/dataset_metadata/{{ dataset.id }}">[Show]</a>
		<div class="dataset_metadata">
		</div> <!-- dataset metadata -->

	      <div class="dataset_datafiles">
              {% if dataset.pk in file_matched_datasets %} 
              <span style="background-color: #FFFF00">
              {% endif %}
              <strong>Data Files ({{ dataset.dataset_file_set.count }}): </strong>
              {% if dataset.pk in file_matched_datasets %}
              </span>
              {% endif %}
		      {% if dataset.dataset_file_set.count|lt:"1000000" %}
                <a class="datafile_list_toggle" href="{% url tardis.tardis_portal.views.retrieve_datafile_list dataset.id %}{% if query %}?query={{ query }}{% endif %}">[Show]</a>
		  {% else %}
		    <a target="_blank" href="{% url tardis.tardis_portal.views.retrieve_datafile_list dataset.id %}">[Show]</a>
		  {% endif %}

		  {% if dataset.dataset_file_set.count|gt:"1000000" %}
		    <br/>
		    <p><em><strong>Note:</strong> Due to large dataset, files will load in a new window</em></p>
		  {% endif %}

		  <ul class="datafile_list">
		  </ul>
	      </div> <!-- dataset_datafiles -->
	    </div> <!-- dataset_extrainfo -->
	  </div> <!-- dataset_info -->
  </div> <!-- dataset -->
      {% endfor %}
    </div> <!-- datasets -->
    <input type="submit" value="Download Selected Files" />
  </form>

</div> <!-- experiment_datasets -->

